package com.example.rent.controller.yx;


import com.example.rent.Result;
import com.example.rent.entity.House;
import com.example.rent.entity.Meeting;
import com.example.rent.service.HouseownerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanted
 * @since 2021-12-02
 */
@RestController
public class HouseownerController {

    @Autowired
    HouseownerService houseownerService;

    @GetMapping("/houseOwnerCenter/Myhouse")
    public Result showMyHouse(@RequestParam("hphone") String hphone) {
        long id = Long.parseLong(hphone);
        try {
            List<House> houses = houseownerService.showMyHouse(id);
            return Result.success(houses);
        } catch (Exception e) {
            return Result.fail();
        }

    }

    @PostMapping("/changePwd")
    public Result changePwd(@RequestParam("hphone") String hphone,
                            @RequestParam("pwd") String pwd) {
        long id = Long.parseLong(hphone);
        try {
            int i = houseownerService.changePwd(id, DigestUtils.md5DigestAsHex(pwd.getBytes()));
            if (i == 1) return Result.success(i);
            else return Result.fail();

        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/houseOwnerCenter/UpdateMyInfo")
    public Result updateMyInfo(@RequestParam("hphone") String hphone,
                               @RequestParam("myName") String myName,
                               @RequestParam("address") String address,
                               @RequestParam("freetime") String freetime) {
        long id = Long.parseLong(hphone);
        try {
            int i = houseownerService.updateMyInfo(id, myName, address, freetime);
            if (i == 1) return Result.success(i);
            else return Result.fail();

        } catch (Exception e) {
            return Result.fail();
        }
    }

    @GetMapping("/newMessageTips")
    public Result newMessageTips(@RequestParam("hphone") String hphone) {
        long id = Long.parseLong(hphone);
        List<Meeting> meetingList = houseownerService.getMessage(id);
        return Result.success(meetingList);
    }

    @GetMapping("/getHouseAddress")
    public Result getHouseAddress(@RequestParam("houseid") String houseid) {
        int houseId = Integer.parseInt(houseid);
        String houseAddress = houseownerService.getHouseAddress(houseId);
        return Result.success(houseAddress);
    }

    @GetMapping("/getMyHouse")
    public Result getMyHouse(@RequestParam("hphone") String hphone) {
        long id = Long.parseLong(hphone);
        try {
            List<House> houses = houseownerService.showMyHouse(id);
            return Result.success(houses);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/updateHouseState")
    public Result updateHouseState(@RequestParam("state") String state,
                                   @RequestParam("address") String address) {
        int s = Integer.parseInt(state);
        try {
            int i = houseownerService.updateHouseState(address, s);
            return Result.success(i);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/submitRepairForm")
    public Result submitRepairForm(@RequestParam("address") String address,
                                   @RequestParam("description") String description) {
        if (address.equals(null)) {
            return Result.fail();
        } else {
            int i = houseownerService.getHouseId(address);
            int j = houseownerService.insertRepair(i, description);
            return Result.success(j);
        }

    }

    @PostMapping("/insertMyHouseInfo")
    public Result insertMyHouseInfo(@RequestParam("hphone") String hphone,
                                    @RequestParam("type") String type,
                                    @RequestParam("livenum") String livenum,
                                    @RequestParam("rent") String rent,
                                    @RequestParam("address") String address,
                                    @RequestParam("state") String state) {
        long id = Long.parseLong(hphone);
        int te = Integer.parseInt(type);
        int lm = Integer.parseInt(livenum);
        float rt = Float.parseFloat(rent);
        int se = Integer.parseInt(state);
        try {
            int i = houseownerService.insertMyHouseInfo(id, te, lm, rt, address, se);
            if (i == 1) {
                return Result.success(i);
            } else {
                return Result.fail();
            }
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/deleteThisHouse")
    public Result deleteThisHouse(@RequestParam("hphone")String hphone,
                                  @RequestParam("address") String address,
                                  @RequestParam("livenum") String livenum){
        long id = Long.parseLong(hphone);
        int lm = Integer.parseInt(livenum);
        int i = houseownerService.deleteThisHouse(id,address,lm);
        if (i == 1){
            return Result.success(i);
        } else {
            return Result.fail();
        }

    }
}

